;(function (w) {
  w.loginPrem = {
    /*登陆权限列表*/
    loginList: function(fn) {
      var _self = this;
      hlhttp.get({
        url: 'session/users/privileges',
        success: function(data) {
          window.localStorage.setItem('permissions', JSON.stringify(data))
          accessControl(fn)
        }
      });
    }
  }

  w.accessControl = function (fn) {
    var prem = JSON.parse(window.localStorage.getItem('permissions'));
    $('.js-access-control').each(function(index, el) {
      var premPoint = $(el).attr('data-control').split(',')
      for (var i = 0; i < prem.length; i++) {
        for(var j = 0; j < premPoint.length; j++){
          if ( premPoint[j] === prem[i] ) {
            $(el).removeClass('js-access-control')
            break
          }
        }
      }
    })

    $('.js-index-nav').each(function (index,el) {
      var flag = true   // 假定该项没有权限显示
      for (var i = 0; i < prem.length; i++) {
        if ( $(el).attr('data-control') === prem[i] ) {
          flag = false
          $(el).removeClass('hidden')
          break
        }
      }
      if (flag) {
        if ($(el).attr('data-control')) {
          $(el).addClass('hidden')
        }
      }
    })

    fn && fn()
  }

  loginPrem.loginList()


  //切换菜单
  function tabNavHome() {

    // 是否展示用户端
    var isShowUserTerminal = function(permissions,type){
      // 如果用户拥有用户端工作台的权限 即有 信息发布模块权限c189 或 定制服务模块权限c259 时，用户可以切换到用户端工作台
      if(type == '业务工作台'){
        if( permissions.indexOf('c189') > -1 || permissions.indexOf('c259') > -1 ){
          $('.work-home').show()
        }else{
          $('.work-home').hide()
        }
      }

      // 如果用户拥有用户端配置的权限 即有 信息发布配置模块权限c189，用户可以切换到用户端配置
      if(type == '配置'){
        if( permissions.indexOf('c203') > -1 ){
          $('.work-cEnd-c0').show()
        }else{
          $('.work-cEnd-c0').hide()
        }
      }
    }

    var handleNavigation = function(permissions){
      $('.tabNav-home b').unbind().on('click', function(event) {
        $(this).addClass('color-primary').siblings('b').removeClass('color-primary');
        if ($(this).text() == '业务工作台') {

          $('.works').show()
          $('.configs').hide()
          $('.work-cEnd-container').hide()
          $('.work-config-cEnd').hide()

          $('.work-cEnd-c').hide()
          $('.work-cEnd-c0').hide()
          $('.work-cEnd-c1').hide()

          isShowUserTerminal(permissions,'业务工作台')
          window.location.href = window.location.protocol+'//' + window.location.host + window.location.pathname + '#/home'
        }
        if ($(this).text() == '配置') {
          $('.works').hide()
          $('.configs').show()
          $('.work-cEnd-container').hide()
          $('.work-config-cEnd').hide()

          $('.work-cEnd-c1').hide()
          $('.work-cEnd-c').hide()
          $('.work-home').hide()

          isShowUserTerminal(permissions,'配置')
          window.location.href = window.location.protocol+'//' + window.location.host + window.location.pathname + '#/companyFrameWork'
        }
      });
    }

    var getPermission = function(){
      var self = this;
      hlhttp.get({
        url: 'session/users/privileges',
        success: function(data) {
          isShowUserTerminal(data,'业务工作台')
          handleNavigation(data)
        }
      });
    }

    getPermission()
  }
  tabNavHome()

  //切换C端工作台
  $('.work-home').unbind().on('click', function() {
    $('.works').hide()
    $('.configs').hide()
    $('.work-cEnd-container').show()
    $('.work-config-cEnd').hide()

    $('.work-cEnd-c').show()
    $('.work-cEnd-c0').hide()
    $('.work-cEnd-c1').hide()
    $('.work-home').hide()
    hlUrlUtil.goTo('cEnd')
  })
  $('.work-cEnd-c').unbind().on('click', function() {
    $('.works').show()
    $('.configs').hide()
    $('.work-cEnd-container').hide()
    $('.work-config-cEnd').hide()

    $('.work-cEnd-c').hide()
    $('.work-cEnd-c0').hide()
    $('.work-cEnd-c1').hide()
    $('.work-home').show()
    hlUrlUtil.goTo('home')
  })
  $('.work-cEnd-c0').unbind().on('click', function() {
    $('.works').hide()
    $('.configs').hide()
    $('.work-cEnd-container').hide()
    $('.work-config-cEnd').show()

    $('.work-cEnd-c').hide()
    $('.work-cEnd-c0').hide()
    $('.work-cEnd-c1').show()
    $('.work-home').hide()
    hlUrlUtil.goTo('cConfig')
  })
  $('.work-cEnd-c1').unbind().on('click', function() {
    $('.works').hide()
    $('.configs').show()
    $('.work-cEnd-container').hide()
    $('.work-config-cEnd').hide()

    $('.work-cEnd-c').hide()
    $('.work-cEnd-c0').show()
    $('.work-cEnd-c1').hide()
    $('.work-home').hide()
    hlUrlUtil.goTo('companyFrameWork')
  })


}(window))
